package longrise.phone.com.bjjt_henan.Adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FrameworkManager;
import com.longrise.bjjt.util.UiUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.CommontUtils;
import longrise.phone.com.bjjt_henan.myaccident.PhotoImageView;

/**
 * Created by Administrator on 2016/5/14.
 */
public class PictureAdapter extends BaseAdapter {

    private Context mContext;

    private List<EntityBean> lists;

    private float screenW;

    private int type;

    public PictureAdapter(Context context, float screenW) {
        this.mContext = context;
        this.screenW = screenW;
    }

    public void setType(int type) {
        this.type = type;
    }

    public void setLists(List<EntityBean> lists) {
        this.lists = lists;
    }

    public void addData(EntityBean bean) {
        if (lists == null) {
            lists = new ArrayList<EntityBean>();
        }
        lists.add(bean);
    }

    @Override
    public int getCount() {
        return (null == lists) ? 0 : lists.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (null == convertView) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_picture_item, null);
            holder.picIV = (ImageView) convertView.findViewById(R.id.picture_iv);
            holder.infoTV = (TextView) convertView.findViewById(R.id.picture_tv);
            holder.infoIV = (ImageView) convertView.findViewById(R.id.picture_iv_info);
            convertView.setTag(holder);

            setImgSize(holder.picIV, 12, 8);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        EntityBean bean = lists.get(position);
        showItem(holder, bean, position);
        if (type == 1) {
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    List<String> PicLists = new ArrayList<String>();
                    for (int i = 0; i < lists.size(); i++) {
                        PicLists.add(lists.get(i).getString("imageurl"));
                    }
                    PhotoImageView view = new PhotoImageView(mContext, PicLists);
                    view.setCurItem(position);
                    FrameworkManager.getInstance().showNewForm(mContext, view);
                }
            });
        }
        return convertView;
    }

    /**
     * 根据屏幕大小设置图片显示的宽高值
     *
     * @param imageView
     * @param padding   距离左右的间距
     * @param space     item中间的间距
     */
    private void setImgSize(ImageView imageView, int padding, int space) {
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
        int rWidth = (int) (screenW - UiUtil.dip2Px(padding * 2 + space)) / 2;
        int height = rWidth * 104 / 170;//切图的宽高比
        params.width = rWidth;
        params.height = height;

        imageView.setLayoutParams(params);
    }

    private void showItem(ViewHolder holder, EntityBean bean, int pos) {
        String desc = bean.getString("desc");
        if (TextUtils.isEmpty(desc)) {
            desc = CommontUtils.getImgTypeStr(bean.getString("imagetype"));
        }
        holder.infoTV.setText(desc);
        //默认图片路径
        String imgPath = "drawable://" + R.mipmap.icon_pic_camera;
        holder.infoIV.setVisibility(View.GONE);
        //网络路径
        String uri = bean.getString("imageurl");
        if (TextUtils.isEmpty(uri)) {
            //本地路径
            String path = bean.getString("path");
            if (TextUtils.isEmpty(path)) {//不存在照片
//                uri = imgPath;
                String type = bean.getString("type");
                if (!TextUtils.equals(type, "-1")) {//补充图片不显示必拍
                    holder.infoIV.setVisibility(View.VISIBLE);
                }
            } else {
                uri = "file://" + path;
            }
        }
        if (TextUtils.isEmpty(uri)) {
            holder.picIV.setImageResource(R.mipmap.icon_pic_camera);
        } else {
            ImageLoader.getInstance().displayImage(uri, holder.picIV, CommontUtils.getOptions());
        }
    }

    //更新指定Item
    public void updateItem(int refreshItemPostion, final View view) {
        if (refreshItemPostion > lists.size()) {
            UiUtil.showToast(mContext, "error：超出列表范围");
            return;
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        showItem(holder, lists.get(refreshItemPostion), refreshItemPostion);
    }

    private class ViewHolder {
        private ImageView picIV;
        private TextView infoTV;
        private ImageView infoIV;
    }
}
